﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyOA.BLL;

namespace MyOA.UI.UserManage
{
    public partial class UserManage : System.Web.UI.Page
    {
        UserInfoManager userInfoManager = new UserInfoManager();
        string message = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            //未登录则先登录
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    dataBind();
                }
            }
        }
        ////绑定用户信息
        //private void DisplayUserInfo()
        //{
        //    this.gvUserInfo.DataSourceID = null;
        //    this.gvUserInfo.DataSource = userInfoManager.GetAllUserInfo();
        //    this.gvUserInfo.DataBind();
        //}

        protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
                ImageButton imgbtnDelete = (ImageButton)e.Row.FindControl("imgbtnDelete");
                imgbtnDelete.Attributes.Add("onclick", "return confirm('确定要删除吗？')");
            }
        }
        protected void gvUserInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;                   //获取命令
            int userId = Convert.ToInt32(e.CommandArgument);      //获取参数
            //命令为删除，则执行删除事件
            if (commandName == "userInfoDelete")
            {
                int result = userInfoManager.DeleteUserInfoByuserId(userId);
                if (result > 0)
                {
                    message = "删除成功";
                }
                else
                {
                    message = "删除失败";
                }
                string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", message);
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
            }
            //命令为修改，则执行修改事件
            if (commandName == "userInfoUpdate")
            {
                Response.Redirect("CreateOrUpdateUserInfo.aspx?userId=" + userId);
            }
            dataBind();
        }
        //添加用户信息
        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateOrUpdateUserInfo.aspx");
        }

        //数据绑定方法
        private void dataBind()
        {
            //实例化数据化对象
            PagedDataSource pgDataSource = new PagedDataSource();
            pgDataSource.DataSource = userInfoManager.GetAllUserInfo();
            anpUserInfo.RecordCount = userInfoManager.GetAllUserInfo().Count();
            pgDataSource.AllowPaging = true;    //启用分页
            pgDataSource.PageSize = anpUserInfo.PageSize;  //单页显示的项数
            pgDataSource.CurrentPageIndex = anpUserInfo.CurrentPageIndex - 1;  //获取当前的索引
            this.gvUserInfo.DataSourceID = null;
            this.gvUserInfo.DataSource = pgDataSource;
            this.gvUserInfo.DataBind();
        }

        protected void anpUserInfo_PageChanged(object sender, EventArgs e)
        {
            dataBind();
        }



    }
}
